/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import logica.Cliente;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import util.HibernateUtil;

/**
 *
 * @author AlisonPC
 */
public class ClienteDAO {

    private Session sessao;
    private Transaction transacao;

    public void salvar(Cliente cliente) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        sessao.save(cliente);
        transacao.commit();
        sessao.close();
    }

    public void remover(Cliente cliente) {
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        sessao.delete(cliente);
        transacao.commit();
        sessao.close();
    }
     public Cliente pesquisaID(int id){
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        Cliente clientes = (Cliente) sessao.createCriteria(Cliente.class).
                add(Restrictions.eq("id", id)).uniqueResult();
        sessao.close();
        return clientes;        
    }
     
     public List<Cliente> pesquisaNomeiLike(String nome){
        sessao = HibernateUtil.getSessionFactory().openSession();
        transacao = sessao.beginTransaction();
        List<Cliente> clientes = (List<Cliente>) sessao.createCriteria(Cliente.class).
                add(Restrictions.ilike("nome", nome, MatchMode.ANYWHERE)).list();
        sessao.close();
        return clientes;        
    }
    
   
    
 
    
}
